package terminal

import (
	"context"
	"device-admin/config/redis"
	"device-admin/internal/define"
	"encoding/json"
	"time"

	"192.168.1.75/go-pkg/go-cache"
)

// sendAlarmMsg 发送告警消息
func SendAlarmMsg(robotID string, alarmType int, alarmContent string) {
	type alarmMsgType struct {
		AlarmSource int    `json:"alarm_source"`
		AlarmType   int    `json:"alarm_type"`
		RobotID     string `json:"robot_id"`
		Battery     string `json:"battery"`
		Content     string `json:"content"`
		CreatedAt   int64  `json:"created_at"`
	}
	var alarmMsg alarmMsgType
	alarmMsg.AlarmType = alarmType
	alarmMsg.RobotID = robotID
	alarmMsg.Content = alarmContent
	alarmMsg.CreatedAt = time.Now().Unix()
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
	defer cancel()
	alarmMsg.Battery = "0"
	bat, _ := cache.HGet(define.CacheRobotRTInfo+robotID, "system_battery")
	alarmMsg.Battery, _ = bat.(string)
	alarmMsg.Battery += "%"
	dataBytes, _ := json.Marshal(alarmMsg)
	redis.NewDB().LPush(ctx, define.RedisAlarmNotifyList, string(dataBytes))
}
